var GraceJS = require("../../Grace.JS/grace.js");
import {
    apiServer
} from '../../common.js';
import grace from '../../Grace.JS/grace.js';
Page({

    /**
     * 页面的初始数据
     */
    data: {
        id: 0,
        detail: '',
        imgArr:[],
    },
    showPic: function(e){
        GraceJS.previewImage(this.data.detail.pic_list, e.currentTarget.dataset.src)
    },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        var that = this;
        if (options.id) {
            that.setData({
                id: options.id
            }, function () {
                that.getInfo();
            })
        }
    },

    getInfo: function () {
        var that = this;
        wx.showLoading({
            title: 'Loading...',
        })
        GraceJS.get(
            apiServer + 'resume.project/detail', {
                id: that.data.id
            }, {},
            function (res) {
                if (res.code == 0) {
                    that.setData({
                        detail: res.data,
                        goodPicTxt: that.formatRichText(res.data.content)
                    })
                    wx.hideLoading()
                } else {
                    GraceJS.msg(res.message)
                }
            }
        )
    },

    /**
     * 处理富文本里的图片宽度自适应
     * 1.去掉img标签里的style、width、height属性
     * 2.img标签添加style属性：max-width:100%;height:auto
     * 3.修改所有style里的width属性为max-width:100%
     * 4.去掉<br/>标签
     */
    formatRichText: function (html) {
        var newContent = html.replace(/<img[^>]*>/gi, function (match, capture) {
            match = match.replace(/style="[^"]+"/gi, '').replace(/style='[^']+'/gi, '');
            match = match.replace(/width="[^"]+"/gi, '').replace(/width='[^']+'/gi, '');
            match = match.replace(/height="[^"]+"/gi, '').replace(/height='[^']+'/gi, '');
            return match;
        });
        newContent = newContent.replace(/style="[^"]+"/gi, function (match, capture) {
            match = match.replace(/width:[^;]+;/gi, 'max-width:100%;').replace(/width:[^;]+;/gi, 'max-width:100%;');
            return match;
        });
        newContent = newContent.replace(/<br[^>]*\/>/gi, '');
        newContent = newContent.replace(/\<img/gi, '<img style="max-width:100%;height:auto;display:block;margin:10px 0;"');

        // 主要代码为后面预览图片准备
        let imgArr = [];
        let regex = new RegExp(/<img.*?(?:>|\/>)/gi); // 匹配所有图片
        let srcReg = /src=[\'\"]?([^\'\"]*)[\'\"]?/i; // 匹配src图片
        let arrsImg = newContent.match(regex); // newContent 后台返回的富文本数据
        if (arrsImg) {
            for (let i = 0; i < arrsImg.length; i++) {
                let srcs = arrsImg[i].match(srcReg);
                imgArr.push(srcs[1])
              }
              this.setData({
                  imgArr : imgArr,
              })
        }

        return newContent;
    },

    // 点击放大预览图片函数
  catchImage(){
    if (this.data.imgArr.length == 0) {
        return false;
    }
    wx.previewImage({
      current: this.data.imgArr[0], // 当前显示图片的http链接
      urls: this.data.imgArr // 需要预览的图片http链接列表
    })
  },
})